//
//  CycleImage.m
//  UI16_CollectionViewCycleImage
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "CycleImage.h"
#import "ViewController.h"
#import "SecViewController.h"
@interface CycleImage () <UICollectionViewDataSource, UICollectionViewDelegate, SecViewControllerDelegate>
@property (nonatomic, strong) UICollectionView *cycleCollectionView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) CGFloat longWith;

@end

@implementation CycleImage

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
        _longWith = 0.0;
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextTime) userInfo:nil repeats:YES];
    }
    return self;
}

//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        self.backgroundColor = [UIColor yellowColor];
//        [self setUp];
//    }
//    return self;
//}

- (void)setUp {
    [self createCollectionView];
    [self createPageControl];
    
}

- (void)createCollectionView {
     _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _cycleCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_layout];
    _cycleCollectionView.pagingEnabled = YES;
    _cycleCollectionView.delegate = self;
    _cycleCollectionView.dataSource = self;
    _cycleCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_cycleCollectionView];
    [_cycleCollectionView registerClass:[CycleImageCell class] forCellWithReuseIdentifier:@"reuse"];
    
//    NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
//    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
//    [runLoop addTimer:timer forMode: NSRunLoopCommonModes];
}
- (void)nextTime {
    if (_page.currentPage == _page.numberOfPages - 1) {
        _page.currentPage = 0;
    } else {
        _page.currentPage++;
    }
    _cycleCollectionView.contentOffset = CGPointMake((_page.currentPage + 1) * _cycleCollectionView.bounds.size.width, 0);
}
- (void)changeImage {
    _longWith = self.bounds.size.width + _longWith;
    [_cycleCollectionView setContentOffset:CGPointMake(_longWith, 0) animated:YES];
//    if (_cycleCollectionView.contentOffset.x == (_array.count - 1) * self.bounds.size.width) {
//        _cycleCollectionView.contentOffset = CGPointMake(self.bounds.size.width * 2, 0);
//        _longWith = 0;
//    }
     if (_cycleCollectionView.contentOffset.x == (_array.count - 1) * _cycleCollectionView.bounds.size.width) {
        //        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
          _longWith = 0;
    }

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CycleImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    cell.cycleImageView.image = [UIImage imageNamed:self.array[indexPath.row]];
    cell.titleLabel.text = self.labelArray[indexPath.row];
    return cell;
}

- (void)createPageControl {
    _page = [[UIPageControl alloc] initWithFrame:CGRectMake(50, self.bounds.size.height - 40, 60, 30)];
    _page.currentPageIndicatorTintColor = [UIColor redColor];
    _page.numberOfPages = 5;
    [self addSubview:_page];
    [_page addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventValueChanged];
}

- (void)changePic:(UIPageControl *)page {
    _cycleCollectionView.contentOffset = CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0);
}


- (void)layoutSubviews {
    [super layoutSubviews];
    _cycleCollectionView.frame = self.bounds;
    _layout.itemSize = self.bounds.size;
    [_cycleCollectionView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
}
//NSTimer *myTimer = [NSTimer  timerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:)userInfo:nilrepeats:NO];
//
//[[NSRunLoop  currentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == 0) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width * (_array.count - 2), 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_array.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } else if (scrollView.contentOffset.x == (_array.count - 1) * _cycleCollectionView.bounds.size.width) {
//        [_cycleCollectionView setContentOffset:CGPointMake(_cycleCollectionView.bounds.size.width, 0)];
        [_cycleCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    if (_cycleCollectionView.contentOffset.x == 0) {
        _page.currentPage = 5;
    } else if (_cycleCollectionView.contentOffset.x == (_array.count - 1) * self.bounds.size.width) {
        _page.currentPage = 1;
    } else {
        _page.currentPage = (_cycleCollectionView.contentOffset.x / self.bounds.size.width) - 1;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ViewController *vc = (ViewController *)self.window.rootViewController;
    SecViewController *sec = [SecViewController new];
    sec.delegate = self;
    sec.index = indexPath.row;
    [vc presentViewController:sec animated:YES completion:nil];
}

- (void)sendWith:(NSInteger)secIndex str:(NSString *)str {
    [self.labelArray removeObjectAtIndex:secIndex];
    [self.labelArray insertObject:str atIndex:secIndex];
    [_cycleCollectionView reloadData];
}

- (void)setArray:(NSArray *)array {
    _array = array;
    [_cycleCollectionView reloadData];
}

- (void)setLabelArray:(NSMutableArray *)labelArray {
    _labelArray = labelArray;
    [_cycleCollectionView reloadData];
}

@end


// 为了使用方便 将自定义cell页写在这里


@implementation CycleImageCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _cycleImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width, 30)];
        _titleLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_cycleImageView];
        [self.contentView addSubview:_titleLabel];
    }
    return self;
}






@end



